//
//  ZYCAttendanceSingleDetailsViewController.m
//  ZDSC
//
//  Created by 张威 on 2021/10/26.
//

#import "ZYCAttendanceSingleDetailsViewController.h"
#import "ZYCAttendanceSingleDetailsCell.h"
#import "ZYCAttendanceSingleDetailsSectionView.h"
#import "ZYCAttendanceSingleDetailsHeaderView.h"
#import "ZYCAttendanceDetailsMenuView.h"
#import "ZYCAttendanceSingleDetailsModel.h"
#import "ZYCSelectPastYearMonthView.h"
#import "NSDateExtra.h"

@interface ZYCAttendanceSingleDetailsViewController ()<UITableViewDataSource,UITableViewDelegate,ZYCAttendanceSingleDetailsSectionViewDelegate,ZYCAttendanceDetailsMenuViewDelegate>
{
    NSIndexPath *_indexPath; // 保存当前选中的单元格
    NSMutableArray *switchArr;
}
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ZYCAttendanceDetailsMenuView *menuView;
@property (nonatomic, strong) ZYCAttendanceSingleDetailsHeaderView *headerView;
@property (nonatomic, strong) NSDateFormatter *dateFormatter;
@property (nonatomic, strong) ZYCAttendanceSingleDetailsModel *detailModel;
@property (nonatomic, strong) ZYCSelectPastYearMonthView *dateSelectView;

@end

@implementation ZYCAttendanceSingleDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = [NSString stringWithFormat:@"%@的考勤明细",self.titleString];
    [self.view addSubview:self.menuView];
    [self.view addSubview:self.tableView];
    NSString *menuStr = [NSString stringWithFormat:@"%@", [NSDateExtra getDateStringWithDateString:self.attendanceDate currenDateFormat:@"yyyy-MM-dd" dateFormat:@"yyyy年MM月"]];
     self.menuView.menuStr = menuStr;
    

    self.tableView.tableHeaderView = self.headerView;
    
    switchArr = [NSMutableArray arrayWithCapacity:0];
    
    
    if (switchArr.count < self.dataSource.count) {
        for (NSDictionary *dict in self.dataSource) {
            if ([dict[@"status"] isEqualToString:self.status]) {
                [switchArr addObject:@YES];
            }else{
                [switchArr addObject:@NO];
            }
        }
        [self.tableView reloadData];
    }
    
    [self loadSingleAttendanceDetail];
}

// 个人考勤明细
- (void)loadSingleAttendanceDetail{
    NSDictionary *params = @{
        @"attendanceDate":self.attendanceDate,
        @"teacherId":self.teacherId
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_TeacherAttendanceInfo requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                self.detailModel = [ZYCAttendanceSingleDetailsModel yy_modelWithDictionary:responseObject[@"body"]];
                self.headerView.model = self.detailModel;
                
                if (self.detailModel.listCD.count > 0) {
                    for (ZYCAttendanceStatusModel *m in self.detailModel.listCD) {
                        m.isShow = YES;
                    }
                    ZYCAttendanceStatusModel * last = self.detailModel.listCD.lastObject;
                    last.isShow = NO;
                }
                if (self.detailModel.listZT.count > 0) {
                    for (ZYCAttendanceStatusModel *m in self.detailModel.listZT) {
                        m.isShow = YES;
                    }
                    ZYCAttendanceStatusModel * last = self.detailModel.listZT.lastObject;
                    last.isShow = NO;
                }
                if (self.detailModel.listQK.count > 0) {
                    NSMutableArray *temp = @[].mutableCopy;
                    [self.detailModel.listQK enumerateObjectsUsingBlock:^(ZYCAttendanceStatusModel * _Nonnull m, NSUInteger idx, BOOL * _Nonnull stop) {
                        m.isShow = YES;
                        if ([m.attendanceStatus integerValue] == 4 && [m.endAttendanceStatus integerValue] == 4) {
                            NSMutableArray *t = [NSMutableArray arrayWithCapacity:0];
                            for (NSInteger i = 0; i < 2; i++) {
                                ZYCAttendanceStatusModel *item = [[ZYCAttendanceStatusModel alloc] init];
                                if (i == 0) {
                                    item.attendanceStatus = @"4";
                                    item.endAttendanceStatus = @"0";
                                }else{
                                    item.attendanceStatus = @"0";
                                    item.endAttendanceStatus = @"4";
                                }
                                item.attendanceDate = m.attendanceDate;
                                item.attendanceId = m.attendanceId;
                                item.attendanceWeek = m.attendanceWeek;
                                item.earlyLeave = m.earlyLeave;
                                item.goOffWorkTime = m.goOffWorkTime;
                                item.goToWorkTime = m.goToWorkTime;
                                item.lateLeave = m.lateLeave;
                                item.leaveEndTime = m.leaveEndTime;
                                item.leaveTime = m.leaveTime;
                                item.workOvertimeLeave = m.workOvertimeLeave;
                                item.isShow = m.isShow;
                                [t addObject:item];
                            }
                            [temp addObjectsFromArray:t];
                        }else{
                            [temp addObject:m];
                        }
//                        [self.detailModel.listQK removeObject:m];
                    }];
          
                    self.detailModel.listQK = temp;
                    ZYCAttendanceStatusModel * last = self.detailModel.listQK.lastObject;
                    last.isShow = NO;
                }
                
                if (self.detailModel.listQJ.count > 0) {
                    for (ZYCAttendanceStatusModel *m in self.detailModel.listQJ) {
                        m.isShow = YES;
                    }
                    ZYCAttendanceStatusModel * last = self.detailModel.listQJ.lastObject;
                    last.isShow = NO;
                }
                
                if (self.detailModel.listJB.count > 0) {
                    for (ZYCAttendanceStatusModel *m in self.detailModel.listJB) {
                        m.isShow = YES;
                    }
                    ZYCAttendanceStatusModel * last = self.detailModel.listJB.lastObject;
                    last.isShow = NO;
                }
                
                [self.tableView reloadData];
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([switchArr[section] boolValue] == NO) {
        return 0;
    }else{
        switch (section) {
            case 0:{
                return self.detailModel.listCD.count;
                break;
            }
            case 1:{
                return self.detailModel.listZT.count;
                break;
            }
            case 2:{
                return self.detailModel.listQK.count;
                break;
            }
            case 3:{
                return self.detailModel.listQJ.count;
                break;
            }
            case 4:{
                return self.detailModel.listJB.count;
                break;
            }
            default:
                return 0;
                break;
        }
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCAttendanceSingleDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCAttendanceSingleDetailsCell class]) forIndexPath:indexPath];
    cell.status = self.dataSource[indexPath.section][@"status"];
    switch (indexPath.section) {
        case 0:{
            cell.model = self.detailModel.listCD[indexPath.row];
            break;
        }
        case 1:{
            cell.model = self.detailModel.listZT[indexPath.row];
            break;
        }
        case 2:{
            cell.model = self.detailModel.listQK[indexPath.row];
            break;
        }
        case 3:{
            cell.model = self.detailModel.listQJ[indexPath.row];
            break;
        }
        case 4:{
            cell.model = self.detailModel.listJB[indexPath.row];
            break;
        }
        default:
            break;
    }

    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 65.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ZYCAttendanceSingleDetailsSectionView *header = [[ZYCAttendanceSingleDetailsSectionView alloc] init];
    header.tag = section;
    NSDictionary *dict = self.dataSource[section];
    header.titleString = dict[@"title"];
    header.colorString = dict[@"color"];
    header.is_unfold = [switchArr[section] boolValue];
    header.delegate = self;
    switch (section) {
        case 0:{//迟到
            header.contentString = [NSString stringWithFormat:@"%@次",self.detailModel.countCD];
            break;
        }
        case 1:{// 早退
            header.contentString = [NSString stringWithFormat:@"%@次",self.detailModel.countZT];
            break;
        }
        case 2:{// 缺卡
            header.contentString = [NSString stringWithFormat:@"%@次",self.detailModel.countQK];
            break;
        }
        case 3:{// 请假
            header.contentString = [NSString stringWithFormat:@"%.1f天",[self.detailModel.durationLeave floatValue]];
            break;
        }
        case 4:{ // 加班
            header.contentString = [NSString stringWithFormat:@"%.1f小时",[self.detailModel.workOvertimeLeave floatValue]];
            break;
        }
        default:
            break;
    }
    return header;
}


#pragma mark - ZYCAttendanceSingleDetailsSectionViewDelegate
- (void)attendanceSingleDetailsSectionViewDidFold:(ZYCAttendanceSingleDetailsSectionView *)view{
    NSInteger section = view.tag;
    if ([switchArr[section] boolValue] == NO) {
        [switchArr replaceObjectAtIndex:section withObject:@YES];
    }
    else {
        [switchArr replaceObjectAtIndex:section withObject:@NO];
    }
    [self.tableView reloadData];
    
}

#pragma mark - ZYCAttendanceDetailsMenuViewDelegate

- (void)attendanceDetailsMenuViewClickMenu{
    [self.dateSelectView show];
}
#pragma mark - getters

- (ZYCSelectPastYearMonthView *)dateSelectView {
    ZYCWeakSelf
    if (!_dateSelectView) {
        _dateSelectView = [[ZYCSelectPastYearMonthView alloc] init];
        _dateSelectView.tipString = @"请选择时间";
        _dateSelectView.currentDate = [NSDate date];
        _dateSelectView.confirmAction = ^(NSString * _Nonnull dateStr) {
            weakSelf.attendanceDate = [NSString stringWithFormat:@"%@-01",dateStr];
            weakSelf.menuView.menuStr = [NSString stringWithFormat:@"%@", [NSDateExtra getDateStringWithDateString:dateStr currenDateFormat:@"yyyy-MM" dateFormat:@"yyyy年MM月"]];
            [weakSelf loadSingleAttendanceDetail];
        };
        [self.navigationController.view addSubview:_dateSelectView];
    }
    return _dateSelectView;
}

- (NSDateFormatter *)dateFormatter {
    if (!_dateFormatter) {
        _dateFormatter = [[NSDateFormatter alloc] init];
        [_dateFormatter setDateFormat:@"yyyy-MM-dd"];
    }
    return _dateFormatter;
}

- (ZYCAttendanceDetailsMenuView *)menuView {
    if (!_menuView) {
        _menuView = [[ZYCAttendanceDetailsMenuView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, 46.0f)];
        _menuView.delegate = self;
    }
    return _menuView;
}
- (ZYCAttendanceSingleDetailsHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[ZYCAttendanceSingleDetailsHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 215.0f)];
    }
    return _headerView;
}


- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.menuView.frame), kScreenWidth, kScreenHeight - CGRectGetMaxY(self.menuView.frame)) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCAttendanceSingleDetailsCell class] forCellReuseIdentifier:NSStringFromClass([ZYCAttendanceSingleDetailsCell class])];
    }
    return _tableView;
}

- (NSArray *)dataSource {
    if (!_dataSource) {
        _dataSource = @[
                        @{@"title":@"迟到",@"status":@"2",@"color":@"#EC4402"},
                        @{@"title":@"早退",@"status":@"3",@"color":@"#41B166"},
                        @{@"title":@"缺卡",@"status":@"4",@"color":@"#2B302D"},
                        @{@"title":@"请假",@"status":@"5",@"color":@"#FDAC5C"},
                        @{@"title":@"加班",@"status":@"6",@"color":@"#39B5A0"},
        ];
    }
    return _dataSource;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
